﻿// -----------------------------------------------------------------------
// <copyright file="GoFiguration.Services.cs" company="Eric Swanson">
// 
// SUMMARY: *Open source and free to use and modify! 
//          (including Commercially)
// 
// Copyright (c) 2012 Eric Swanson
// 
// Permission is hereby granted, free of charge, to any person obtaining 
// a copy of this software and associated documentation files (the 
// "Software"), to deal in the Software without restriction, including 
// without limitation the rights to use, copy, modify, merge, publish, 
// distribute, sublicense, and/or sell copies of the Software, and to 
// permit persons to whom the Software is furnished to do so, subject to 
// the following conditions:
// 
// The above copyright notice and this permission notice shall be 
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
// SOFTWARE.
// </copyright>
// -----------------------------------------------------------------------

namespace GoFiguration.Services

open System.IO
open System.Runtime.Serialization.Json
open System.ServiceModel
open System.ServiceModel.Activation
open System.ServiceModel.Web
open System.Xml.Serialization

open GoFiguration

/// <summary>
/// Contract for a GoFigure WCF service.
/// </summary>
[<ServiceContract(ConfigurationName = "GoFigure.IGoFigureService", Namespace = "http://gofigure.codeplex.com")>]
type IGoFigureService =

    /// <summary>
    /// Gets the configuration setting value as XML using the specified <paramref name="key"/>.
    /// </summary>
    /// <param name="key">The setting key.</param>
    /// <returns>The configuration setting value as an XML <see cref="System.String"/>.</returns>
    [<OperationContract>]
    abstract GetAsXML : key:string -> string

    /// <summary>
    /// Gets the configuration setting value as JSON using the specified <paramref name="key"/>.
    /// </summary>
    /// <param name="key">The setting key.</param>
    /// <returns>The configuration setting value as a JSON <see cref="System.String"/>.</returns>
    [<OperationContract>]
    abstract GetAsJSON : key:string -> string

/// <summary>
/// The GoFigure WCF service.
/// </summary>
[<AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)>]
[<ServiceBehavior(Namespace = "http://gofigure.codeplex.com", InstanceContextMode = InstanceContextMode.PerCall)>]
type GoFigureService() =

    interface IGoFigureService with

        /// <summary>
        /// Gets the configuration setting value as XML using the specified <paramref name="key"/>.
        /// </summary>
        /// <param name="key">The setting key.</param>
        /// <returns>The configuration setting value as an XML <see cref="System.String"/>.</returns>
        override this.GetAsXML key:string =
            match GoFigure.ContainsKey(key) with
            | false -> null
            | _ ->
                let value = GoFigure.Get<obj>(key, null)
                let stream = new MemoryStream()
                let serializer = new XmlSerializer(value.GetType())
                serializer.Serialize(stream, value)
                stream.Position <- int64 0
                let reader = new StreamReader(stream)
                let result = reader.ReadToEnd()
                result

        /// <summary>
        /// Gets the configuration setting value as a JSON <see cref="System.String"/> using the specified <paramref name="key"/>.
        /// </summary>
        /// <param name="key">The setting key as JSON.</param>
        /// <returns>The configuration setting value as a JSON <see cref="System.String"/>.</returns>
        [<WebGet(UriTemplate = "{*key}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)>]
        override this.GetAsJSON key:string =
            match GoFigure.ContainsKey(key) with
            | false -> null
            | _ ->
                let value = GoFigure.Get<obj>(key, null)
                let stream = new MemoryStream()
                let serializer = new DataContractJsonSerializer(value.GetType())
                serializer.WriteObject(stream, value)
                stream.Position <- int64 0
                let reader = new StreamReader(stream)
                let result = reader.ReadToEnd()
                result